package commSql

import (
	"fmt"
	"gitee.com/fierce_wolf/go-fox-edge-common/commUtil/String"
	"reflect"
)

type foxSqlService struct {
}

func (e *foxSqlService) SelectOption1List(tableName string, underField1 string, toCamelCase bool) ([]map[string]interface{}, error) {
	field := FieldValue{}
	field.Name = underField1
	field.Value = ""

	fields := make([]FieldValue, 0)
	fields = append(fields, field)

	sb := "SELECT DISTINCT " + underField1 + " FROM " + tableName
	return e.SelectMapList(sb, fields, toCamelCase, make([]string, 0))
}

func (e *foxSqlService) SelectOption2List(tableName string, underField1 string, underField2 string, value1 interface{}, toCamelCase bool) ([]map[string]interface{}, error) {
	sb := ""
	sb += "SELECT DISTINCT " + underField1 + ", " + underField2 + " FROM " + tableName + " WHERE "
	if value1 == nil {
		sb += underField1 + " IS NULL"
	} else if reflect.TypeOf(value1).Kind() == reflect.String {
		sb += (underField1) + " = '" + value1.(string) + "'"
	} else {
		sb += underField1 + " = " + fmt.Sprintf("%v", value1)
	}

	fields := make([]FieldValue, 0)

	field1 := FieldValue{}
	field1.Name = underField1
	field1.Value = ""
	fields = append(fields, field1)

	field2 := FieldValue{}
	field2.Name = underField2
	field2.Value = ""
	fields = append(fields, field2)

	return e.SelectMapList(sb, fields, toCamelCase, make([]string, 0))
}

func (e *foxSqlService) SelectOptionList3(tableName string, underField1 string, underField2 string, underField3 string, value1 interface{}, toCamelCase bool) ([]map[string]interface{}, error) {
	sb := ""
	sb += "SELECT DISTINCT " + underField1 + ", " + underField2 + ", " + underField3 + " FROM " + tableName + " WHERE "
	if value1 == nil {
		sb += underField1 + " IS NULL"
	} else if reflect.TypeOf(value1).Kind() == reflect.String {
		sb += (underField1) + " = '" + value1.(string) + "'"
	} else {
		sb += underField1 + " = " + fmt.Sprintf("%v", value1)
	}

	fields := make([]FieldValue, 0)

	field1 := FieldValue{}
	field1.Name = underField1
	field1.Value = ""
	fields = append(fields, field1)

	field2 := FieldValue{}
	field2.Name = underField2
	field2.Value = ""
	fields = append(fields, field2)

	field3 := FieldValue{}
	field3.Name = underField3
	field3.Value = ""
	fields = append(fields, field3)

	return e.SelectMapList(sb, fields, toCamelCase, make([]string, 0))
}

func (e *foxSqlService) SelectOptionList(tableName string, underField1 string, filters map[string]interface{}, toCamelCase bool) ([]map[string]interface{}, error) {
	sb := ""
	sb += "SELECT DISTINCT " + underField1 + " FROM " + tableName + " WHERE  1=1 "

	for key,value := range filters{
		sb += " AND ";

		if (value == nil) {
			sb += key + " IS NULL";
		} else if (reflect.TypeOf(value).Kind() == reflect.String) {
			sb += key + " = '" + value.(string) + "'";
		} else {
			sb += key + " = " + String.ToString(value);
		}
	}

	fields := make([]FieldValue, 0)
	field := FieldValue{}
	field.Name = underField1
	field.Value = ""
	fields = append(fields, field)

	return e.SelectMapList(sb, fields, toCamelCase, make([]string, 0))
}

func (e *foxSqlService) SelectMapList(selectSql string, fields []FieldValue, toCamelCase bool, jsn2obj []string) ([]map[string]interface{}, error) {
	mapList, err := Client.QueryList(selectSql, fields)
	if err != nil {
		return nil, err
	}

	// 驼峰转换
	if toCamelCase {
		return e.toCamelCase(mapList), nil
	}

	return mapList, nil
}

func (e *foxSqlService) toCamelCase(mapList []map[string]interface{}) []map[string]interface{} {
	resultList := make([]map[string]interface{}, 0)

	for _, mp := range mapList {
		result := make(map[string]interface{})
		for key, v := range mp {
			result[String.CamelName(key)] = v
		}
		resultList = append(resultList, result)
	}

	return resultList
}
